//
//  WSRequirementView.m
//  LANGirl
//
//  Created by 吴帅 on 16/3/30.
//
//

#import "WSRequirementView.h"
#import "AppDelegate.h"

@interface WSRequirementView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *RequirementTableView;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIView *shareBackGroundView;

@end

@implementation WSRequirementView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        self.backView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.backView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
        self.shareBackGroundView = self.backView;
        
        
        AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
        [delegate.window addSubview:self.shareBackGroundView];
        self.backView.hidden = NO;
        [self.backView addSubview:self];
        
        
        self.titleLabel = [[UILabel alloc] init];
        [self addSubview:_titleLabel];
        self.titleLabel.text = @"报酬";
        self.titleLabel.textColor = HS;
        self.titleLabel.font = [UIFont systemFontOfSize:TEXTCONTENTFONT2];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.mas_equalTo(@0);
            make.height.mas_equalTo(30);
            make.top.mas_equalTo(0);
        }];
        
        
        UIView *lineView = [[UIView alloc] init];
        [self addSubview:lineView];
        lineView.backgroundColor = LINECOlOR;
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.right.mas_equalTo(@0);
            make.bottom.equalTo(self.titleLabel.mas_bottom);
            make.height.mas_equalTo(@1);
        }];
        
        self.RequirementTableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
        self.RequirementTableView.delegate = self;
        self.RequirementTableView.dataSource = self;
        self.RequirementTableView.scrollEnabled = NO;
        [self addSubview:_RequirementTableView];
        [self.RequirementTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        [self.RequirementTableView mas_makeConstraints:^(MASConstraintMaker *make) {

            make.top.mas_equalTo(30);
            make.left.right.bottom.mas_equalTo(@0);
            
        }];
        
       
        UIView *tempView = [UIView new];
        [self.backView addSubview:tempView];
        [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.right.left.mas_equalTo(@0);
            make.bottom.equalTo(self.mas_top);
        }];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [tempView addGestureRecognizer:tap];
    
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.text = _dataArr[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:TEXTCONTENTFONT];
    cell.textLabel.textColor = TextLightGrayColor;
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(15, 0, 15, 0)];
    }
    // Prevent the cell from inheriting the Table View's margin settings
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    // Explictly set your cell's layout margins
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsMake(15, 0, 15, 0)];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *dataStr = _dataArr[indexPath.row];
    [self.delegate getSelectData:dataStr];
    
   
        [self disMissView];

    
    }

- (void)setDataArr:(NSArray *)dataArr {

    _dataArr = dataArr;
    self.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, _dataArr.count * 44 + 30);
}

- (void)showView {
    
    self.backView.hidden = NO;
    CGRect frame = self.frame;
    frame.origin.y = SCREEN_HEIGHT - frame.size.height;
    [UIView animateWithDuration:0.3 animations:^{
        
        
        self.frame = frame;
        self.backView.backgroundColor=[UIColor colorWithWhite:0.0 alpha:0.3];
        
    }];
}

- (void)tapAction:(UITapGestureRecognizer *)tap {

    [self.delegate getSelectData:nil];
    [self disMissView];
}

- (void)disMissView {
   
    CGRect frame = self.frame;
    frame.origin.y = SCREEN_HEIGHT;
    [UIView animateWithDuration:0.3 animations:^{
        
        self.frame = frame;
        self.backView.backgroundColor=[UIColor colorWithWhite:0.0 alpha:0.0];
        
    } completion:^(BOOL finished) {
        
        self.backView.hidden = YES;
    }];
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
